package cn.qitom.gateway;

import cn.qitom.core.launch.LegoSpringApplication;
import cn.qitom.gateway.props.DynamicAllowProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author lengleng
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigurationProperties({DynamicAllowProperties.class})
public class GatewayApplication {

	public static void main(String[] args) {
		LegoSpringApplication.run(GatewayApplication.class,args);

	}


	@Bean
	@LoadBalanced
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}
	@RestController
	class EchoController {


		@Autowired
		private RestTemplate restTemplate;

		@GetMapping("/echo/{string}")
		public String echo(@PathVariable String string) {
			return restTemplate.getForObject("http://lego-user/echo/"+string,String.class);
		}

		@GetMapping("/divide")
		public String divide(@RequestParam Integer a, @RequestParam Integer b) {
			return String.valueOf(a / b);
		}

	}

}
